package com.itheima.health.controller.admin;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.result.Result;
import com.itheima.health.service.SetmealService;
import com.itheima.health.util.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private AliOssUtil  aliOssUtil;

    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    public Result   add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        setmealService.add(setmeal,checkgroupIds);
          return  new Result(true, MessageConst.ADD_SETMEAL_SUCCESS);
    }


    @PostMapping("/upload")
    public   Result  upload(MultipartFile imgFile){
        log.info("文件上传：{}",imgFile);
        String originalFilename = imgFile.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String name = UUID.randomUUID().toString() + extension;

        try {
            String filePath = aliOssUtil.upload(imgFile.getBytes(), name);
            return  new Result(true, MessageConst.PIC_UPLOAD_SUCCESS,filePath);
        } catch (IOException e) {
            log.error("文件上传失败");
        }

        return  new Result(false, MessageConst.PIC_UPLOAD_FAIL);
    }

    @GetMapping
    public   Result  selectAll(){
      List<Setmeal> setmealList = setmealService.seletAll();
        return  new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS,setmealList);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
   public   Result  pageQuery(QueryPageBean queryPageBean){
      PageResult  pageResult =  setmealService.pageQuery(queryPageBean);
       return  new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS,pageResult);
   }




}
